using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyBulletBoss1 : MonoBehaviour
{
    public Vector2 fireDirection=new Vector2(-1,1);
    public float minFireForce = 100f;
    public float maxFireForce = 300f;
    public LayerMask groundLayer;
    public GameObject boomPrefab;
    private Rigidbody2D rig;


    private void Awake()
    {
        rig = GetComponent<Rigidbody2D>();
    }

    private void Start()
    {
        fireDirection.Normalize();
        rig.AddForce(fireDirection * Random.Range(minFireForce,maxFireForce));
    }

    private void FixedUpdate()
    {
        UpdateOverViewportBotom();
        UpdateCollidedGround();
    }

    private bool CheckOverViewportBotom()
    {
        Vector3 viewportBottom = Camera.main.ViewportToWorldPoint(Vector3.zero);
        Vector2 position = transform.position;
        position += Vector2.up * 1f;
        if (viewportBottom.y >= position.y)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private void UpdateOverViewportBotom()
    {
        if (CheckOverViewportBotom())
        {
            Destroy(gameObject);
        }
    }
    private void UpdateCollidedGround()
    {
        Vector2 position = transform.position;
        position += Vector2.down * 0.3125f;
        RaycastHit2D hit=Physics2D.Raycast(position, Vector2.down, 0.1f, groundLayer);
        if (hit)
        {
            Vector2 nextPosition = hit.point + Vector2.down * 1f;
            RaycastHit2D nextHit = Physics2D.Raycast(nextPosition, Vector2.down, 15f, groundLayer);
            if (!nextHit)
            {
                if (boomPrefab != null)
                {
                    Instantiate(boomPrefab, position, Quaternion.identity);
                }
                Destroy(gameObject);
            }
        }
    }

}
